package a2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;

public class GKAA2 {

    public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("src\\Beispielgraph.graph"));
        String str = null;
        String whatgraph = br.readLine();
        SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> sg = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
                
        while ((str = br.readLine()) != null) {
                String[] parts = str.split(",");
				sg.addVertex(parts[0]);
                sg.addVertex(parts[1]);
                DefaultWeightedEdge edge = sg.addEdge(parts[0], parts[1]);
                sg.setEdgeWeight(edge, Double.parseDouble(parts[2]));
        }
		ArrayList<String> cities = new ArrayList<String>(sg.vertexSet());
		Algorithm fw = new FloydWarshal();
		Algorithm di = new Dijkstra();
		
		
		String source;
		String target;
		Random rand = new Random();
		for (int i = 0; i < 1000; ++i) {
			source = cities.get(rand.nextInt(cities.size())); //Zufällige Start und Zielstädte ermitteln
			target = cities.get(rand.nextInt(cities.size()));
			List<String> shortestWay = fw.getShortestWay(sg, source, target);
			List<String> shortestWay2 = di.getShortestWay(sg, source, target);
			if (shortestWay.size() > 0 || shortestWay2.size() > 0) {
				System.out.println(source + " -> " + target);
				
				List<String> way = fw.getShortestWay(sg, source, target);
				List<String> way2 = di.getShortestWay(sg, source, target);
				
				System.out.println("        " + way + "      " + way2);
				System.out.println("         FloydWarshal      Dijkstra");	
				System.out.println("Weglänge: " + getWayLength(sg, way) + "         " + getWayLength(sg, way2));
				System.out.println();
			}
		}
    }
	
	public static double getWayLength(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> graph, List<String> way) {
		double length = 0;
		if (way.size() > 1) {
			for(int i = 0; i < way.size()-1;) {
				length += graph.getEdgeWeight(graph.getEdge(way.get(i), way.get(++i)));
			}
		}
		return length;
	}
}
